<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of contacto
 *
 * @author Federico
 */
class Imagenes extends CI_Controller
{

  public function ver($id=0) 
  {
      $data["imagen"] = new Imagen($id);
        
      $this->smartyview->render("imagen_ver.html", $data);
  } // end function edit


  public function editar($id=0) {
    
    $data["imagen"]   = new Imagen($id);
    $data['usuarios'] = new Usuario();
    $data['usuarios']->get()->all;

    $this->smartyview->render("imagen_edit.html", $data);
  } // end function edit


  public function crear() {

    $data['usuarios'] = new Usuario();
    $data['usuarios']->get()->all;

    $data['tags'] = new Tag();
    $data['tags']->get()->all;

    $this->smartyview->render("imagen_crear.html", $data);
  } // end function edit


  public function crearajax() {

    $data['usuarios'] = new Usuario();
    $data['usuarios']->get()->all;

    $data['tags'] = new Tag();
    $data['tags']->get()->all;

    $this->smartyview->render("imagen_crear_ajax.html", $data);
  } // end function edit


  public function upload() {
    $allowedExtensions = array();
    $sizeLimit = 500 * 1024 * 1024; // MB

    $this->load->library("qqFileUploader");
    $this->qqfileuploader->initialize($allowedExtensions, $sizeLimit);
    $result = $this->qqfileuploader->handleUpload('uploads/');

    echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);
  }

  public function info() {
    die(phpinfo());
  }


  public function save() {

    $usuario             = new Usuario($this->input->post('usuario'));
    $imagen              = new Imagen($this->input->post("id"));
    $imagen->descripcion = $this->input->post("descripcion");
    $tag = new Tag();

    foreach ($this->input->post("tags") as $tagID) {
      $tag->or_where('id',$tagID);
    }
    $tag->get();

    if ( ! $imagen->id ) {

      $this->load->library('upload');

      $config['upload_path']    = './uploads/';
      $config['allowed_types']  = 'gif|jpg|png';
      $config['max_size']       = '400';

      $this->upload->initialize($config); 

      if ($this->upload->do_upload("archivo")) {
        $archivo_subido = $this->upload->data();
      }
      else {
        $error = array('error' => $this->upload->display_errors());
        die(var_dump($error));
      }

      $this->load->library('image_lib');

      $config['source_image'] = $archivo_subido['full_path'];
      $config['width']  = 100;
      $config['height'] = 100;
      $config['new_image'] = './uploads/nuevo.jpg';

      $this->image_lib->initialize($config); 
      $this->image_lib->resize();

      $this->image_lib->clear();      

      $config['source_image'] = './uploads/nuevo.jpg';
      $config['new_image'] = './uploads/rotado.jpg';
      $config['rotation_angle'] = '90';

      $this->image_lib->initialize($config); 
      $this->image_lib->rotate();

      $imagen->ruta  = $archivo_subido["file_name"];
      $imagen->fecha = date("Y-m-d");
    }

    $imagen->save(array($usuario,$tag));

    redirect(site_url("imagenes/ver/".$imagen->id."/"));
  } // end function save






  public function save_ajax() {

    $usuario             = new Usuario($this->input->post('usuario'));
    $imagen              = new Imagen($this->input->post("id"));
    $imagen->descripcion = $this->input->post("descripcion");
    $tag = new Tag();

    foreach ($this->input->post("tags") as $tagID) {
      $tag->or_where('id',$tagID);
    }
    $tag->get();

    if ( ! $imagen->id ) {

      $config['upload_path']    = './uploads/';
      $config['allowed_types']  = 'gif|jpg|png';
      $config['max_size']       = '500';
      $config['max_width']      = '1024';
      $config['max_height']     = '768';

      $this->load->library('upload', $config);

      if ( ! $this->upload->do_upload("archivo")) {
        $error = array('error' => $this->upload->display_errors());
        die(var_dump($error));
      }
      else {
        $data = $this->upload->data();
      }

      $imagen->ruta  = $data["file_name"];
      $imagen->fecha = date("Y-m-d");
    }

    $imagen->save(array($usuario,$tag));
    redirect(site_url("imagenes/ver/".$imagen->id."/"));
  } // end function save

} // class Imagenes

